package com.execdemo.demomasterservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.execdemo.demomasterservice.domain.Store;
import com.execdemo.demomasterservice.domain.StoreExce;
import com.execdemo.demomasterservice.mapper.StoreMapper;
import com.execdemo.demomasterservice.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author tiejingji
 * @description 针对表【store】的数据库操作Service实现
 * @createDate 2025-02-07 15:34:39
 */
@Slf4j
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");


    @Override
    public Boolean saveExcel(MultipartFile file) throws IOException {


        EasyExcel.read(file.getInputStream(), StoreExce.class, new PageReadListener<StoreExce>(dataList -> {
            for (StoreExce storeExce : dataList) {

                String code = storeExce.getCode();
                Store store = getStore(storeExce);
                //mybatis by code
                Store dbStore = storeMapper.selectOne(new LambdaQueryWrapper<Store>().eq(Store::getCode, code));
                log.info("读取到一条数据{}", JSON.toJSONString(storeExce));
                if (dbStore != null) {
                    dbStore.setName(store.getName());
                    dbStore.setOpeningTime(store.getOpeningTime());
                    dbStore.setClosingTime(store.getClosingTime());
                    dbStore.setActiveAccount(store.getActiveAccount());
                    dbStore.setAccountStatus(store.getAccountStatus());
                    storeMapper.updateById(dbStore);
                    redisTemplate.opsForValue().set("storeCode." + code, JSON.toJSONString(dbStore));
                    log.info("数据更新完成{}", JSON.toJSONString(store));

                } else {
                    int insert = storeMapper.insert(store);
                    if (insert != 0) {
                        redisTemplate.opsForValue().set("storeCode." + code, JSON.toJSONString(store));
                        log.info("数据新增完成{}", JSON.toJSONString(store));

                    }else {
                        log.info("数据新增失败{}", JSON.toJSONString(store));
                    }

                }

            }
        })).sheet().doRead();

        return true;
    }

    Store getStore(StoreExce storeExce){
        Store store = new Store();
        store.setCode(storeExce.getCode());
        store.setName(storeExce.getName());
        store.setOpeningTime(LocalTime.parse(storeExce.getOpeningTime(),formatter));
        store.setClosingTime(LocalTime.parse(storeExce.getClosingTime(),formatter));
        store.setActiveAccount(Float.valueOf(storeExce.getActiveAccount()));
        store.setAccountStatus(Integer.parseInt(storeExce.getAccountStatus()));
        return store;
    }

    @Override
    public Boolean renewRedis(String code) {
        Store store = storeMapper.selectOne(new LambdaQueryWrapper<Store>().eq(Store::getCode, code));
        if (store != null) {
            redisTemplate.delete("storeCode." + code);
            redisTemplate.opsForValue().set("storeCode." + code, JSON.toJSONString(store));
            return true;
        }
        return false;
    }
}




